//
// Created by jsjxsy on 2022/8/22.
//
#include <fstream>
#include <iostream>
using namespace std;
const char *file = "../data/2.txt";
bool readWriteAppendFile() {
  ifstream fin;
  fin.open(file);
  char ch;
  if (fin.is_open()) {
    while (fin.get(ch)) {
      cout << ch;
    }
    fin.close();
  } else {
    perror("file open fail:");
  }

  ofstream fout(file, ios_base::app);
  if (fout.is_open()) {
    string name;
    cout << "Enter guest names (enter a blank line to quit)" << endl;
    while (getline(cin, name) && name.size() > 0) {
      fout << name << endl;
    }
    fout.close();
  } else {
    perror("file open fail:");
    exit(EXIT_FAILURE);
  }
  fin.clear();
  fin.open(file);
  if (fin.is_open()) {
    char ch;
    while (fin.get(ch)) {
      cout << ch;
    }
    fin.close();
  } else {
    perror("file open fail:");
  }
  cout << "Done!" << endl;
}

int main() {
  readWriteAppendFile();
  return 0;
}